/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.LinkedList;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import modelo.ModeloTablaPedidos;
import modelo.PacientesTableModel;
import persistencia.PacientesDAO;
import persistencia.PedidosDAO;
import pojos.Paciente;
import pojos.Pedido;

/**
 *
 * @author alejandrolopezdelolmo
 */
public class PanelGestionPaciente extends javax.swing.JPanel {

    /**
     * Creates new form PanelSearchBook
     */
    PacientesDAO pDAO = new PacientesDAO();
    List<String> nombreColumnas = new LinkedList();

    public PanelGestionPaciente(JFrame ventanaPadre) {
        initComponents();
        this.setSize(800, 600);
        rellenarComboBoxPacientes();
        rellenarColumnas();

    }

    private void rellenarComboBoxPacientes() {
        List<Integer> listaCodigos = this.pDAO.getCodigosPacientes();

        DefaultComboBoxModel modeloCodPacientes = new DefaultComboBoxModel();

        for (int i = 0; i < listaCodigos.size(); i++) {
            modeloCodPacientes.addElement(listaCodigos.get(i));

        }

        this.cbxCodPacientes.setModel(modeloCodPacientes);
    }

    private void rellenarColumnas() {
        this.nombreColumnas.add("CÓDIGO DE PACIENTE");
        this.nombreColumnas.add("DNI");
        this.nombreColumnas.add("NOMBRE");
        this.nombreColumnas.add("APELLIDO1");
        this.nombreColumnas.add("APELLIDO2");
        this.nombreColumnas.add("DIRECCION");
        this.nombreColumnas.add("CP");
        this.nombreColumnas.add("POBLACION");

    }

    private void realizarBusqueda() {
        boolean checkCodPaciente = this.selectCodigoPaciente.isSelected();
        boolean checkDNI = this.selectDni.isSelected();
        Integer codPaciente = (Integer) this.cbxCodPacientes.getSelectedItem();
        String dni = this.txtDni.getText();
        List<Paciente> listaPacientes;

        if (checkCodPaciente && checkDNI) {
            listaPacientes = this.pDAO.getPacientePorCodYDni(codPaciente, dni);
        } else if (checkCodPaciente && !checkDNI) {
            listaPacientes = this.pDAO.getPacienteporCodigo(codPaciente);
        } else if (!checkCodPaciente && checkDNI) {
            listaPacientes = this.pDAO.getPacientesPordni(dni);
        } else {
            listaPacientes = this.pDAO.getPacientes();
        }

        this.jTablePacientes.setModel(new PacientesTableModel(listaPacientes, nombreColumnas));
    }

    private void modificarPaciente() {
        int registroSeleccionado = this.jTablePacientes.getSelectedRow();

        if (registroSeleccionado != -1) {

            //COGEMOS EL CODIGO DEL PEDIDO A RECEPCIONAR
            Integer codPacienteModificar = (Integer) this.jTablePacientes.getValueAt(registroSeleccionado, 0);
            Paciente paceinteModificar = this.pDAO.getPacienteporCodigo(codPacienteModificar).get(0);
            JDialogModificarPaciente jdModificarPaciente = new JDialogModificarPaciente(this, paceinteModificar, pDAO);
            jdModificarPaciente.setVisible(true);

        } else {
            JOptionPane.showMessageDialog(this, "DEBES SELECCIONAR UN PEDIDO DE LA TABLA PARA PODER MODIFICARLO", "ERROR AL MODIFICAR", JOptionPane.ERROR_MESSAGE);
        }

    }

    private void eliminarPedido() {
        int registroSeleccionado = this.jTablePacientes.getSelectedRow();

        if (registroSeleccionado != -1) {

            int opcion = JOptionPane.showConfirmDialog(this, "¿ESTAS SEGURO QUE DESEAS ELIMINAR ESTE PACIENTE?", "ELIMINAR PACIENTE", JOptionPane.INFORMATION_MESSAGE);

            if (opcion == JOptionPane.OK_OPTION) {
                Integer codPacienteEliminar = (Integer) this.jTablePacientes.getValueAt(registroSeleccionado, 0);

                Paciente paceinteEliminar = this.pDAO.getPacienteporCodigo(codPacienteEliminar).get(0);
                this.pDAO.eliminarPaciente(paceinteEliminar);

                JOptionPane.showMessageDialog(this, "EL PACIENTE HA SIDO ELIMINADO CON EXITO", "ELIMINACION COMPLETADA", JOptionPane.INFORMATION_MESSAGE);
                realizarBusqueda();
            }

        } else {
            JOptionPane.showMessageDialog(this, "DEBES SELECCIONAR UN PACIENTE DE LA TABLA PARA PODER ELIMINARLO", "ERROR AL ELIMINAR", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void refrescarComboBox() {
        this.cbxCodPacientes.removeAllItems();
        rellenarComboBoxPacientes();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPfiltros = new javax.swing.JPanel();
        selectCodigoPaciente = new javax.swing.JCheckBox();
        cbxCodPacientes = new javax.swing.JComboBox();
        selectDni = new javax.swing.JCheckBox();
        txtDni = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTablePacientes = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        jPfiltros.setBorder(javax.swing.BorderFactory.createTitledBorder("Filtros de Búsqueda"));
        jPfiltros.setLayout(new java.awt.GridLayout(2, 2));

        selectCodigoPaciente.setText("Codigo Paciente");
        jPfiltros.add(selectCodigoPaciente);

        cbxCodPacientes.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jPfiltros.add(cbxCodPacientes);

        selectDni.setText("Dni");
        jPfiltros.add(selectDni);
        jPfiltros.add(txtDni);

        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setLayout(new java.awt.GridLayout(1, 0));

        jTablePacientes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cod Paciente", "Dni", "Nombre", "Apellido1", "Apellido2", "Direccion", "CP", "Poblacion"
            }
        ));
        jScrollPane1.setViewportView(jTablePacientes);

        jPanel1.add(jScrollPane1);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_plus.png"))); // NOI18N
        jButton2.setToolTipText("Agregar");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_edit1.png"))); // NOI18N
        jButton4.setToolTipText("Modificar");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton4);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_find.png"))); // NOI18N
        jButton5.setToolTipText("Consultar");
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton5);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconos/ico_bin.png"))); // NOI18N
        jButton3.setToolTipText("Borrar");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPfiltros, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPfiltros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JDialogAgregarPaciente ap = new JDialogAgregarPaciente();
        ap.setVisible(true);
        refrescarComboBox();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        modificarPaciente();
        realizarBusqueda();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        eliminarPedido();
        refrescarComboBox();
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        try {
            realizarBusqueda();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "No hay ningun paciente");
        }

    }//GEN-LAST:event_jButton5ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cbxCodPacientes;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPfiltros;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JTable jTablePacientes;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JCheckBox selectCodigoPaciente;
    private javax.swing.JCheckBox selectDni;
    private javax.swing.JTextField txtDni;
    // End of variables declaration//GEN-END:variables
}
